# -*- coding: utf-8 -*-
from typing import Dict, Tuple
import os
from wx import ArtProvider, Bitmap
import wx

from infrastructure.constant.Image_size_enum import ImageSizeEnum
from assets.icon import get_icon_path
from infrastructure.utils.image_utils import str2wximg, Str2PilImgConfigEnum

__bitMapImageTypeDict = {
    "png": wx.BITMAP_TYPE_PNG,
    "icon": wx.BITMAP_TYPE_ICON,
    "jpeg": wx.BITMAP_TYPE_JPEG,
    "jpg": wx.BITMAP_TYPE_JPEG,
    "gif": wx.BITMAP_TYPE_GIF,
    "tif": wx.BITMAP_TYPE_TIF
}


def GetBitMapImageType(image_path:str):
    return __bitMapImageTypeDict.get(image_path.split(".")[1].lower())


class WxBitmap:
    bitmaps:Dict[str, Dict[str, Dict[str, Bitmap]]] = dict()

    @classmethod
    def init_bitmaps(cls):
        if cls.bitmaps is None or len(cls.bitmaps) == 0:
            cls.bitmaps = {
                "default": {
                    ImageSizeEnum.s16X16.name: ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_BUTTON)
                },
                "go_back": {
                    ImageSizeEnum.s16X16.name: ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_BUTTON)
                },
                "go_forward": {
                    ImageSizeEnum.s16X16.name: ArtProvider.GetBitmap(wx.ART_GO_FORWARD, wx.ART_BUTTON)
                },
                "go_parent": {
                    ImageSizeEnum.s16X16.name: ArtProvider.GetBitmap(wx.ART_GO_TO_PARENT, wx.ART_BUTTON)
                },
                "open": {
                    ImageSizeEnum.s16X16.name: ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_BUTTON)
                },
                "save": {
                    ImageSizeEnum.s16X16.name: ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_BUTTON)
                },
                "view": {
                    ImageSizeEnum.s16X16.name: ArtProvider.GetBitmap(wx.ART_REPORT_VIEW, wx.ART_BUTTON)
                }
            }

    @classmethod
    def get(cls, name: str, size: ImageSizeEnum=ImageSizeEnum.s16X16):
        cls.init_bitmaps()
        if name is None or size is None:
            return None
        # 用库中的图标
        icon_path = get_icon_path(name, size)
        if icon_path is not None:
            return wx.Bitmap(icon_path, GetBitMapImageType(icon_path))
        # 用这里配置的图标
        items = cls.bitmaps.get(name)
        if items and len(items) > 0:
            bitmap = items.get(size.name, None)
            if bitmap:
                return bitmap
        # 返回默认:文字图片
        image_config = Str2PilImgConfigEnum.get(Str2PilImgConfigEnum.get_name(name[0], size=size))
        return str2wximg(name[0], config=image_config).ConvertToBitmap()